"""
Problem 91: https://projecteuler.net/problem=91

The points P (x1, y1) and Q (x2, y2) are plotted at integer coordinates and
are joined to the origin, O(0,0), to form ΔOPQ.

There are exactly fourteen triangles containing a right angle that can be formed
when each coordinate lies between 0 and 2 inclusive; that is,
0 ≤ x1, y1, x2, y2 ≤ 2.

Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?
"""


def solution(limit: int = 50) -> int:
    """
    assume x1 <= x2
    >>> print(solution(1))
    3
    >>> print(solution(2))
    14
    """
    res = 0
    for x1 in range(limit+1):
        for y1 in range(limit+1):
            for x2 in range(x1, limit+1):
                for y2 in range(limit+1):
                    if x1*y2 == x2*y1:
                        continue
                    else:
                        dx = x2-x1
                        dy = y2-y1
                        if any([x1*x2+y1*y2 == 0, x1*dx+y1*dy == 0, x2*dx+y2*dy == 0]):
                            if x1==y2:
                                if y1<=y2:
                                    # print((x1, y1), (x2, y2))
                                    res+=1
                            else:
                                # print((x1, y1), (x2, y2))
                                res+=1

    return res


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    print(solution())
    # 14126
